package com.test.schoolhelper.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.test.schoolhelper.R;
import com.test.schoolhelper.entity.User;
import com.test.schoolhelper.utils.PhoneUtils;

import java.util.ArrayList;
import java.util.List;

// TODO: 2024/5/29 联系人
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    private Context context;
    private List<User> list = new ArrayList<>();

    public UserAdapter(Context context, List<User> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        User user= list.get(position);
        String name= user.getName();
        holder.tvName.setText(name);
        holder.tvComment.setText(name.substring(name.length()-1));

        holder.rlMain.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(position);
            }
        });

        holder.ivPhone.setOnClickListener(v -> {
            PhoneUtils.callPhone2(context,user.getPhone());

        });

        holder.ivMsg.setOnClickListener(v -> {
            PhoneUtils.SendSMS(context,user.getPhone(),"");
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rlMain;
        private TextView tvComment;
        private TextView tvName;
        private ImageView ivPhone;
        private ImageView ivMsg;

        public ViewHolder(View itemView) {
            super(itemView);
            rlMain = itemView.findViewById(R.id.rl_main);
            tvComment = itemView.findViewById(R.id.tv_comment);
            tvName = itemView.findViewById(R.id.tv_name);
            ivMsg = itemView.findViewById(R.id.iv_msg);
            ivPhone = itemView.findViewById(R.id.iv_phone);
        }
    }

    private OnClickListener listener;

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener {
        void onClick(int pos);
    }

}
